package com.levy.utils.jvisualVM;

import org.junit.Test;

/**
 * Created by levy on 2019/6/27.
 */
public class ThreadDemo2 {

    //必须是final，否则会报错
    private static final long count = 100000000;

    @Test
    public void testEfficiency() throws Exception {

        serial();         //串行
        concurrence();         //并行
    }

    private void concurrence() throws InterruptedException {
        long start = System.currentTimeMillis();
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                int a = 0;
                for(long i =0;i<count;i++){
                    a ++;
                    a ++;
                    a ++;
                }

            }
        });

        t.start();

        int b = 0;
        for(long i = 0;i<count;i++){
            b --;
            b --;
            b --;
        }
        t.join();

        long end = System.currentTimeMillis();
        System.out.println("并行执行时长："+(end - start));

    }

    private void serial() {
        long start = System.currentTimeMillis();
        int a = 0;
        for(long i=0;i<count;i++){
            a ++;
        }
        int b = 0;
        for(long i=0;i<count;i++){
            b --;
        }
        long end = System.currentTimeMillis();
        System.out.println("串行执行时长："+(end - start));
    }
}
